﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Windows.Media.Imaging;
using System.Windows.Media.Effects;

using HelixEngine;
using HelixEngine.Render;
using HelixEngine.Maths;

namespace HelixGame.World.Entities.Components
{
    /// <summary>
    /// 
    /// </summary>
    public class GraphicAppearenceComponent : EntityComponent
    {
        SceneManager _targetScene;

        /// <summary>
        /// 
        /// </summary>
        public SceneManager TargetScene
        {
            get { return _targetScene; }
            set { _targetScene = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnInitialised()
        {
            _targetScene = Engine.Get().RenderManager.RootScene;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public void CreateNewTargetScene(string name)
        {
            _targetScene = Engine.Get().RenderManager.CreateScene(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pixelWidth"></param>
        /// <param name="pixelHeight"></param>
        public void CreateNewTargetScene(string name, int pixelWidth, int pixelHeight)
        {
            _targetScene = Engine.Get().RenderManager.CreateScene(name, pixelWidth, pixelHeight);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="blendMode"></param>
        public void QueueWrite(SceneManager scene, WriteableBitmapExtensions.BlendMode blendMode = WriteableBitmapExtensions.BlendMode.Alpha)
        {
            _targetScene.QueueWrite(scene.Depth, scene.RenderTarget, Vector2.Zero, blendMode);
        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="zIndex"></param>
        /// <param name="bitmap"></param>
        /// <param name="destPosition"></param>
        /// <param name="blendMode"></param>
        public void QueueWrite(int zIndex, WriteableBitmap bitmap, Vector2 destPosition, WriteableBitmapExtensions.BlendMode blendMode = WriteableBitmapExtensions.BlendMode.Alpha)
        {
            _targetScene.QueueWrite(zIndex, bitmap, destPosition, blendMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="zIndex"></param>
        /// <param name="bitmap"></param>
        /// <param name="blendMode"></param>
        public void QueueWrite(int zIndex, WriteableBitmap bitmap, WriteableBitmapExtensions.BlendMode blendMode = WriteableBitmapExtensions.BlendMode.Alpha)
        {
            _targetScene.QueueWrite(zIndex, bitmap, Vector2.Zero, blendMode);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Render()
        {
            _targetScene.ExecuteQueuedWrites();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="blendMode"></param>
        public void QueueToRootScene(WriteableBitmapExtensions.BlendMode blendMode = WriteableBitmapExtensions.BlendMode.Alpha)
        {
            Engine.Get().RenderManager.RootScene.QueueWrite(_targetScene, blendMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="destPosition"></param>
        /// <param name="blendMode"></param>
        public void QueueToRootScene(Vector2 destPosition, WriteableBitmapExtensions.BlendMode blendMode = WriteableBitmapExtensions.BlendMode.Alpha)
        {
            Engine.Get().RenderManager.RootScene.QueueWrite(_targetScene, blendMode, destPosition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void ApplyShader<T>() where T : ShaderEffect, new()
        {
            _targetScene.ApplyShader<T>();
        }
    }
}
